package bme.andraskindler.treenventory;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.OpenCVLoader;

import android.app.Application;
import android.graphics.Bitmap;
import android.widget.Toast;
import bme.andraskindler.treenventory.imagemanipulation.ImageProcessor;
import bme.andraskindler.treenventory.model.CameraData;
import bme.andraskindler.treenventory.model.TypeData;
import bme.andraskindler.treenventory.provider.BluetoothService;
import bme.andraskindler.treenventory.provider.TreenventoryDataSource;

import com.activeandroid.ActiveAndroid;

/**
 * @author andraskindler
 * 
 *         Ez az osztály az Application osztály leszármazottja, az alkalmazás indulásakor példányosodik. Singleton patternre épül, elsősorban inicializációs funkciói vannak, az alkalmazás indulásakor inicializálom az OpenCV-t. Lekérhető tőle a készülék API szintje, a Bluetooth szolgáltatás és a kamerával lőtt kép tárolására is használom.
 */
public class TreenventoryApplication extends Application {

	private static TreenventoryApplication mInstance;
	private BluetoothService mService;
	private String mConnectedDeviceName;
	private static final int CURRENT_API_VERSION = android.os.Build.VERSION.SDK_INT;

	private static Bitmap mCameraImage;

	private boolean isConnected;

	@Override
	public void onCreate() {
		super.onCreate();

		OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, new BaseLoaderCallback(this) {

			@Override
			public void onManagerConnected(int status) {
				super.onManagerConnected(status);
				CameraData cam = TreenventoryDataSource.getCameraParams();
				if (cam != null)
					System.out.println(ImageProcessor.matToString(cam.getCamera()));
			}

		});

		ActiveAndroid.initialize(this);
		setConnectedDeviceName("");
		mInstance = this;
		isConnected = false;

		if (TreenventoryDataSource.getAllTypes(false).size() == 0) {
			TypeData data1 = new TypeData("Kocsanyos tolgy", "Quercus robur");
			TypeData data2 = new TypeData("Kozonseges lucfenyo", "Picea abies");
			TreenventoryDataSource.persistType(data1);
			TreenventoryDataSource.persistType(data2);
		}

	}

	public static int getCurrentApiVersion() {
		return CURRENT_API_VERSION;
	}

	public static void displayMessage(String msg) {
		Toast.makeText(mInstance, msg, Toast.LENGTH_SHORT).show();
	}

	public static TreenventoryApplication getInstance() {
		return mInstance;
	}

	public BluetoothService getmService() {
		if (mService == null)
			mService = new BluetoothService();
		return mService;
	}

	public void setmService(BluetoothService mService) {
		this.mService = mService;
	}

	public String getConnectedDeviceName() {
		if (mConnectedDeviceName == "")
			return "nincs kapcsolat";
		return "kapcsolódva: " + mConnectedDeviceName;
	}

	public void setConnectedDeviceName(String mConnectedDeviceName) {
		this.mConnectedDeviceName = mConnectedDeviceName;
	}

	public boolean isConnected() {
		return isConnected;
	}

	public void setConnected(boolean isConnected) {
		this.isConnected = isConnected;
	}

	public static Bitmap getCameraImage() {
		return mCameraImage;
	}

	public static void setCameraImage(Bitmap mCameraImage) {
		TreenventoryApplication.mCameraImage = mCameraImage;
	}

}
